#pragma once

#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Comm.hpp"

class Inetaddr
{
private:
	void PostNet_Host()
	{
		_post = ::ntohs(_net_addr.sin_port);
	}
	void IpNet_Host()
	{
		char ipbuffer[64];
		const char *ip = ::inet_ntop(AF_INET, &_net_addr.sin_addr, ipbuffer, sizeof(ipbuffer));
		_ip = ipbuffer;
	}

public:
	Inetaddr() {}
	Inetaddr(const struct sockaddr_in &addr) : _net_addr(addr)
	{
		PostNet_Host();
		IpNet_Host();
	}
	Inetaddr(const uint16_t &post) : _ip(""), _post(post)
	{
		_net_addr.sin_family = AF_INET;
		_net_addr.sin_port = ::htons(_post);
		_net_addr.sin_addr.s_addr = INADDR_ANY;
	}
	struct sockaddr *Netaddr() { return CONV(&_net_addr); }
	socklen_t NetaddrLen() { return sizeof(_net_addr); }
	uint16_t post() { return _post; }
	std::string IP() { return _ip; }
	std::string addr()
	{
		return IP() + " : " + std::to_string(post());
	}
	bool operator==(const Inetaddr &addr)
	{
		return _ip == addr._ip && _post == addr._post;
	}

private:
	struct sockaddr_in _net_addr;
	std::string _ip;
	uint16_t _post;
};